package com.photography.wedding.controller;

import com.photography.wedding.pojo.Menus;
import com.photography.wedding.pojo.Picture;
import com.photography.wedding.service.MenusService;
import com.photography.wedding.service.PhotoService;
import com.photography.wedding.service.UserService;
import com.photography.wedding.util.DateUtil;
import com.photography.wedding.util.ReturnUtil;
import com.photography.wedding.util.UuidUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RequestMapping
@RestController
public class PhotoJsonController {
    @Autowired
    private PhotoService photoService;
    @Autowired
    private MenusService menusService;

    @ResponseBody
    @RequestMapping(value = "/member/addPhoto",method = RequestMethod.POST)
    public ModelMap addPhoto(@Validated @RequestBody Picture picture, BindingResult result){
        try {
            if(StringUtils.isEmpty(picture.getUserId())){
                return ReturnUtil.error("用户ID不能为空", null, null);
            }

            picture.setId(UuidUtil.runVerifyCode(10));

            int count = photoService.insertSelective(picture);
            if (count == 0){
                return ReturnUtil.error("添加图片失败");
            }

            return ReturnUtil.success("添加图片成功");
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }

    }

    @ResponseBody
    @RequestMapping(value = "/member/selectUserPicture",method = RequestMethod.POST)
    public ModelMap selectUserPicture(@Validated @RequestBody Picture picture, BindingResult result){
        try {
            if(StringUtils.isEmpty(picture.getUserId())){
                return ReturnUtil.error("用户ID不能为空", null, null);
            }

            List<Picture> pictureList = photoService.selectUserPicture(picture.getUserId());

            if (pictureList.size() == 0){
                return ReturnUtil.error("暂无数据信息");
            }

            return ReturnUtil.success("查询图片成功", pictureList);
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }

    @ResponseBody
    @RequestMapping(value = "/member/selectAdminPictureAll",method = RequestMethod.POST)
    public ModelMap selectAdminPictureAll(@Validated @RequestBody Picture picture, BindingResult result){
        try {
            if(StringUtils.isEmpty(picture.getUserLevel())){
                return ReturnUtil.error("用户标识不能为空", null, null);
            }

            List<Picture> pictureList = photoService.selectAdminPictureAll(picture.getUserLevel());

            if (pictureList.size() == 0){
                return ReturnUtil.error("暂无数据信息");
            }

            return ReturnUtil.success("查询图片成功", pictureList);
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }


    @ResponseBody
    @RequestMapping(value = "/member/selectUserPictureModuleName",method = RequestMethod.POST)
    public ModelMap selectUserPictureModuleName(@Validated @RequestBody Picture picture, BindingResult result){
        try {
            if(StringUtils.isEmpty(picture.getUserId())){
                return ReturnUtil.error("用户ID不能为空", null, null);
            }

            if(StringUtils.isEmpty(picture.getModuleName())){
                return ReturnUtil.error("文件名不能为空", null, null);
            }

            List<Picture> pictureList = photoService.selectUserPictureModuleName(picture);

            if (pictureList.size() == 0){
                return ReturnUtil.error("暂无数据信息");
            }

            return ReturnUtil.success("查询图片成功", pictureList);
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }


    @ResponseBody
    @RequestMapping(value = "/member/delatePhoto",method = RequestMethod.POST)
    public ModelMap delatePhoto(@Validated @RequestBody Picture picture, BindingResult result){
        try {
            if(StringUtils.isEmpty(picture.getId())){
                return ReturnUtil.error("图片ID不能为空", null, null);
            }

            int count = photoService.deleteByPrimaryKey(picture.getId());
            if (count == 0){
                return ReturnUtil.error("删除图片失败");
            }

            return ReturnUtil.success("删除图片成功");
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }

    }

    @ResponseBody
    @RequestMapping(value = "/member/deleteUserPicture",method = RequestMethod.POST)
    public ModelMap deleteUserPicture(@Validated @RequestBody Picture picture, BindingResult result){
        try {

            if(StringUtils.isEmpty(picture.getUserId())){
                return ReturnUtil.error("用户ID不能为空", null, null);
            }

            if(StringUtils.isEmpty(picture.getModuleName())){
                return ReturnUtil.error("文件名不能为空", null, null);
            }


            List<Picture> pictureList = photoService.selectUserPictureModuleName(picture);
            if (pictureList.size() != 0){
                int count = photoService.deleteUserPicture(picture);
                if (count == 0){
                    return ReturnUtil.error("删除图片失败");
                }
            }else {
                return ReturnUtil.success("暂无图片数据");
            }



            return ReturnUtil.success("删除图片成功");
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }

    }


    @ResponseBody
    @RequestMapping(value = "/member/selectUserLevelModuleNamePicture",method = RequestMethod.POST)
    public ModelMap selectUserLevelModuleNamePicture(@Validated @RequestBody Picture picture, BindingResult result){
        try {
            if(StringUtils.isEmpty(picture.getUserLevel())){
                return ReturnUtil.error("用户标识不能为空", null, null);
            }

            if(StringUtils.isEmpty(picture.getModuleName())){
                return ReturnUtil.error("文件名不能为空", null, null);
            }

            List<Picture> pictureList = photoService.selectUserLevelModuleNamePicture(picture);

            if (pictureList.size() == 0){
                return ReturnUtil.error("暂无数据信息");
            }

            return ReturnUtil.success("查询图片成功", pictureList);
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }

    @ResponseBody
    @RequestMapping(value = "/member/selectByPrimaryKey",method = RequestMethod.POST)
    public ModelMap selectByPrimaryKey(@Validated @RequestBody Picture picture, BindingResult result){
        try {
            if(StringUtils.isEmpty(picture.getId())){
                return ReturnUtil.error("图片ID不能为空", null, null);
            }


            Picture picture1 = photoService.selectByPrimaryKey(picture.getId());

            if (picture1 == null){
                return ReturnUtil.error("暂无数据信息");
            }

            return ReturnUtil.success("查询图片成功", picture1);
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }

    @ResponseBody
    @RequestMapping(value = "/member/updatePhoto",method = RequestMethod.POST)
    public ModelMap updatePhoto(@Validated @RequestBody Picture picture, BindingResult result){
        try {

            if(StringUtils.isEmpty(picture.getId())){
                return ReturnUtil.error("图片ID不能为空", null, null);
            }


            int count = photoService.updateByPrimaryKeySelective(picture);
            if (count == 0){
                return ReturnUtil.error("更新图片失败");
            }

            return ReturnUtil.success("更新图片成功");
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }

    }


    @ResponseBody
    @RequestMapping(value = "/member/selectMenusAndPicture",method = RequestMethod.POST)
    public ModelMap selectMenusAndPicture(@Validated @RequestBody Picture picture, BindingResult result){
        try {
            List<Menus> menusList = menusService.selectAdminMenuAll("0");
            List<Picture> pictureList = photoService.selectAdminPictureAll("0");

            for (int j = 0; j < menusList.size(); j++){
                List<Picture> pictureList1 = new ArrayList<>();
                for (int i = 0; i < pictureList.size(); i++){
                    if (menusList.get(j).getModuleName().equals(pictureList.get(i).getModuleName())){
                        pictureList1.add(pictureList.get(i));
                    }
                }
                menusList.get(j).setPictureList(pictureList1);
            }

            return ReturnUtil.success("获取数据成功", menusList);

        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }


}
